package it.chengdazhi.decentbanner;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;

import java.util.ArrayList;
import java.util.List;

public class DecentBannerAdapter extends PageSliderProvider {

    List<Component> viewList;
    private int tabNum;
    private int itemNum;

    public DecentBannerAdapter(int itemNum, List<Component> views) {
        viewList = new ArrayList<>(tabNum);
        viewList = views;
        this.itemNum = itemNum;
        this.tabNum = views.size();
    }

    @Override
    public int getCount() {
        return itemNum;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, final int i) {
        Component view = viewList.get(i % tabNum);
        if(view.getComponentParent() != null) {
            view.getComponentParent().removeComponent(view);
        }
        componentContainer.addComponent(view);
        return componentContainer;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent(viewList.get(i % tabNum));
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }

}
